#coding:utf-8
import tornado.httpserver
import tornado.ioloop
import tornado.web
import tornado.escape
from tornado.options import define,options
from config import settings
from handlers.main.main_urls import handlers
from models.account.account_user_model import User
from libs.db import create_tables
from libs.db.dbsession import dbSession

define('port',default=9003,help='run port',type=int)
define('runserver',default=False,help='start server',type=bool)
define('t',default=False,help='create_table',type=bool)
define('u',default=False,help='create_user',type=bool)

if __name__=='__main__':
    options.parse_command_line()
    if options.t:
        create_tables.run()
    if options.u:
        user=User()
        user.name='lvbo'
        user.password='lvbo'
        dbSession.add(user)
        dbSession.commit()

    if options.runserver:
        app=tornado.web.Application(handlers,**settings)
        http_server=tornado.httpserver.HTTPServer(app)
        http_server.listen(options.port)
        print 'Start Server...'
        tornado.ioloop.IOLoop.instance().start()